﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;


namespace GT.BlogBox.SecurityControl
{
    class SecurityControl:WebControl
    {

        protected override void OnLoad(EventArgs e)
        {

            base.OnLoad(e);

            if (SPContext.Current.Web.CurrentUser==null & !Page.Request.Url.AbsoluteUri.EndsWith("default.aspx"))
            {
                
                List<string> negativeMatches = new List<string>();
                negativeMatches.Add("AllItems.aspx");
                negativeMatches.Add("/_layouts/");
                negativeMatches.Add("/lists/");

                List<string> positiveMatches = new List<string>();
                positiveMatches.Add("/lists/Posts/Post.aspx");
                positiveMatches.Add("/lists/Categories/Category.aspx");
                positiveMatches.Add("/lists/Posts/Date.aspx");
                positiveMatches.Add("/lists/Posts/Monthlyarchive.aspx");
                positiveMatches.Add("/_layouts/OSSSearchResults.aspx");

                bool matched = false;

                foreach (string s in negativeMatches)
                {
                    matched = Regex.IsMatch(Page.Request.Url.AbsoluteUri, s, RegexOptions.IgnoreCase);
                    if (matched) break;
                }

                if (matched)
                {
                    foreach (string s in positiveMatches)
                    {
                        matched = !Regex.IsMatch(Page.Request.Url.AbsoluteUri, s, RegexOptions.IgnoreCase);
                        if (!matched) break;
                    }
                }

                if (matched)
                {
                    SPWeb web = SPContext.Current.Web;
                    Page.Response.Redirect(web.Url);
                }

            }

        }

    }
}
